﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormDeliver
    Dim objCA As New clsCPCK
    Dim prod_ids As String
    Dim dtCk As New DataTable
    Public usr As String
    Dim objProdInfo As New clsProdInfo
    Dim httpHelper As New HttpHelper
    Private Sub FormDeliver_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        initDt()
        txtProdId.Focus()
    End Sub

    Public Sub initDt()
        dtCk.Columns.Add("location")
        dtCk.Columns.Add("area")
        dtCk.Columns.Add("area_name")
        dtCk.Columns.Add("status")
        dtCk.Columns.Add("status_name")
        dtCk.Columns.Add("prod_id")
        dtCk.Columns.Add("prod_spec_name")
        dtCk.Columns.Add("steel_seal_no")
        dtCk.Columns.Add("into_date")
        dtCk.Columns.Add("into_ope")
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    Dim area_nam As String = String.Empty
                    If dt.Rows(0)("area") = "ZAGZ01" Then
                        area_nam = "隔震成品库"
                    End If
                    If dt.Rows(0)("area") = "ZAGZ01A" Then
                        area_nam = "隔震成品库_A区"
                    End If
                    If dt.Rows(0)("area") = "ZAGZ01B" Then
                        area_nam = "隔震成品库_B区"
                    End If
                    If dt.Rows(0)("area") = "ZAGZ01C" Then
                        area_nam = "隔震成品库_C区"
                    End If
                    If dt.Rows(0)("area") = "ZAGZ01D" Then
                        area_nam = "隔震成品库_D区"
                    End If
                    If dt.Rows(0)("area") = "ZAGZ01E" Then
                        area_nam = "隔震成品库_E区"
                    End If
                    Dim status_nam As String = String.Empty
                    If dt.Rows(0)("status") = "0" Then
                        status_nam = "在库"
                    Else
                        status_nam = "已出库"
                    End If
                    Dim dr As DataRow() = dtCk.Select("prod_id='" & dt.Rows(0)("prod_id") & "'")
                    If dr.Length = 0 Then
                        dtCk.Rows.Add(dt.Rows(0)("location"), dt.Rows(0)("area"), area_nam, dt.Rows(0)("status"), status_nam, dt.Rows(0)("prod_id"),
                        dt.Rows(0)("prod_spec_name"), dt.Rows(0)("steel_seal_no"), dt.Rows(0)("into_date"), dt.Rows(0)("into_ope"))
                        prod_ids &= "'" & dt.Rows(0)("prod_id") & "',"
                        dgv.AutoGenerateColumns = False
                        dgv.DataSource = dtCk
                        lblRowCount.Text = dgv.Rows.Count
                        txtProdId.Text = String.Empty
                        txtProdId.Focus()
                    Else
                        MsgBox("该标签号已在表格中存在！", vbYes, "提示")
                    End If
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到此生产编号库存信息")
                        txtProdId.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub btnOUT_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If String.IsNullOrEmpty(usr) = False Then
            If dtCk.Rows.Count > 0 Then
                If MsgBox("确认进行出库吗？", vbYesNo, "提示") = vbYes Then
                    If prod_ids.Length > 0 Then
                        prod_ids = prod_ids.Substring(0, prod_ids.Length - 1)
                    End If
                    Dim rtn As Integer = objCA.outCK(prod_ids, usr)
                    If rtn <> -2 Then
                        CType(Owner, FormMain).setDgvData()
                        MsgBox("保存成功", MsgBoxStyle.Information, "成品仓库")
                        dtCk.Rows.Clear()
                        txtProdId.Focus()
                    Else
                        MsgBox("保存失败", MsgBoxStyle.Information, "成品仓库")
                    End If
                End If
            Else
                MsgBox("请扫描或输入标签号，进行出库", vbYes, "成品仓库")
            End If
        Else
            MsgBox("请勾选用户", MsgBoxStyle.Information, "成品仓库")
        End If
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        If dgv.SelectedRows.Count > 0 Then
            dtCk.Rows.RemoveAt(dgv.CurrentRow.Index)
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtCk
            lblRowCount.Text = dgv.Rows.Count
        End If
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Dim rtn As String = HttpHelper.HttpPostWebService("materialsConsumption", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function
End Class